﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Cos.Core.Module.Response
{
    [JsonObject(MemberSerialization.OptIn)]
    public class CameleonResult<TData> : CameleonResult
    {
        [JsonProperty("response")]
        public TData ResponseData { get; set; }

        public CameleonResult(TData result, CameleonResultStatus status)
            : base(status)
        {
            this.ResponseData = result;
        }
    }

    [JsonObject(MemberSerialization.OptIn)]
    public class CameleonResult : ActionResult
    {
        public string ContentType { get; set; }

        public Encoding ContentEncoding { get; set; }

        [JsonConverter(typeof(StringEnumConverter))]
        [JsonProperty("result")]
        public CameleonResultStatus Status { get; set; }

        [JsonProperty("message")]
        public string Message { get; set; }

        public CameleonResult(CameleonResultStatus status)
        {
            this.Status = status;
        }

        public override void ExecuteResult(ControllerContext context)
        {
            var response = context.HttpContext.Response;

            if (this.ContentEncoding != null)
            {
                response.ContentEncoding = this.ContentEncoding;
            }

            response.ContentType = this.ContentType;
            response.Write(JsonConvert.SerializeObject(this, Formatting.Indented));
        }
    }
}
